package com.frxuexi.shop.view

import android.util.Log
import android.widget.SearchView.OnQueryTextListener
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.lib_base.entity.HomeBean
import com.frxuexi.lib_base.entity.ShopBean
import com.frxuexi.lib_base.entity.ShopBeanItem
import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.shop.intent.ShopIntent
import com.frxuexi.shop.viewmodel.ShopViewModel
import com.frxuexi.shop.adapter.ShopAdapter
import com.frxuexi.shop.databinding.ActivityShopBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class ShopActivity : BaseActivity<ActivityShopBinding, ShopIntent, ShopViewModel>() {
    override val binding: ActivityShopBinding by lazy { ActivityShopBinding.inflate(layoutInflater) }
    override val vm: ShopViewModel by viewModels<ShopViewModel>()
    val shopAdapter by lazy { ShopAdapter() }
    val list= mutableListOf<ShopBeanItem>()
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                Log.i("lr",it.data.toString())
                                val homeBean = it.data as HomeBean
                                binding.tabShop.apply {
                                    homeBean.explosiveMoney.forEach {
                                        addTab(newTab().setText(it.name))
                                    }
                                    addOnTabSelectedListener(object :OnTabSelectedListener{
                                        override fun onTabSelected(tab: TabLayout.Tab?) {
                                            vm.sendIntent(
                                                ShopIntent.getShops(
                                                    homeBean.explosiveMoney[tab?.position!!].type.toInt(),
                                                    1
                                                )
                                            )
                                        }

                                        override fun onTabUnselected(tab: TabLayout.Tab?) {

                                        }

                                        override fun onTabReselected(tab: TabLayout.Tab?) {

                                        }
                                    })
                                }

                                vm.sendIntent(
                                    ShopIntent.getShops(
                                        homeBean.explosiveMoney[0].type.toInt(),
                                        1
                                    )
                                )
                            }
                            RequestType.SHOPS->{
                                val shopBean = it.data as ShopBean
                                list.clear()
                                list.addAll(shopBean.list)
                                shopAdapter.apply {
                                    data.clear()
                                    addData(shopBean.list)
                                    notifyDataSetChanged()
                                }
                            }
                            RequestType.SELECT_KEY->{
                                val shopBean = it.data as ShopBean
                                shopAdapter.apply {
                                    data.clear()
                                    addData(shopBean.list)
                                    notifyDataSetChanged()
                                }
                            }

                            else -> {}
                        }

                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.rvShop.apply {
            adapter=shopAdapter
            layoutManager=StaggeredGridLayoutManager(2,RecyclerView.VERTICAL)
        }
        vm.sendIntent(ShopIntent.getHomeData)

        shopAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/shop/ShopDetailActivity").withInt("id",shopAdapter.data.get(position).id).navigation()
        }

        binding.shopSv.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if(query!=null) {
                    vm.sendIntent(ShopIntent.getShopsByKey(query!!))
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                shopAdapter.apply {
                    data.clear()
                    addData(list)
                    notifyDataSetChanged()
                }
                return true
            }
        })
    }

}